﻿using UnityEngine;

namespace Jinndev {

    /// <summary>
    /// 对象池工具类，用于安全的调用对象池相关接口
    /// </summary>
    public static class PoolUtil {

        /// <summary>
        /// 实例化指定预设，如果对象池启用且存在对应的预设，则使用对象池创建实例，否则尝试直接实例化预设
        /// </summary>
        /// <param name="prefab">预设物</param>
        /// <param name="parent">创建到父物体下</param>
        /// <param name="worldPositionStays"></param>
        /// <returns></returns>
        public static GameObject Instantiate(GameObject prefab, Transform parent = null, bool worldPositionStays = false) {
            if (PoolManager.Instance != null) {
                GameObject obj = PoolManager.Instance.Instantiate(prefab, parent, worldPositionStays);
                if (obj != null) {
                    return obj;
                }
            }
            return Object.Instantiate(prefab, parent, worldPositionStays);
        }

        /// <summary>
        /// 实例化指定预设，如果对象池启用且存在对应的预设，则使用对象池创建实例，否则尝试直接实例化预设
        /// </summary>
        /// <param name="prefabPath">预设路径</param>
        /// <param name="parent">创建到父物体下</param>
        /// <param name="worldPositionStays"></param>
        public static GameObject Instantiate(string prefabPath, Transform parent = null, bool worldPositionStays = false) {
            if (PoolManager.Instance != null) {
                GameObject obj = PoolManager.Instance.Instantiate(prefabPath, parent, worldPositionStays);
                if (obj != null) {
                    return obj;
                }
            }
            GameObject prefab = Resources.Load<GameObject>(prefabPath);
            return prefab == null ? null : Object.Instantiate(prefab, parent, worldPositionStays);
        }

        /// <summary>
        /// 尝试销毁/回收对象
        /// </summary>
        public static void Destroy(GameObject gameObject) {
            if (CommonUtil.IsQuitting || gameObject == null) {
                return;
            }
            if (PoolManager.Instance == null) {
                // 如果没有启用对象池，则直接销毁
                Object.Destroy(gameObject);
            }
            else {
                // 找到实例对象
                PoolItem item = gameObject.GetComponent<PoolItem>();
                if (item != null) {
                    // 回收
                    item.Recycle();
                }
                else {
                    // 销毁
                    Object.Destroy(gameObject);
                }
            }
        }

        /// <summary>
        /// 尝试销毁/回收对象
        /// </summary>
        public static void Destroy(MonoBehaviour behaviour) {
            if (CommonUtil.IsQuitting || behaviour == null || behaviour.gameObject == null) {
                return;
            }
            if (PoolManager.Instance == null) {
                // 如果没有启用对象池，则直接销毁
                Object.Destroy(behaviour.gameObject);
            }
            else {
                // 找到实例对象
                PoolItem item = behaviour.GetComponent<PoolItem>();
                if (item != null) {
                    // 回收
                    item.Recycle();
                }
                else {
                    // 销毁
                    Object.Destroy(behaviour.gameObject);
                }
            }
        }

    }

}